#coding=utf-8
import MySQLdb

MYSQLSERVER={
    "host":"10.100.112.44",
    "user":"booker",
    "passwd":"dg@#d1567",
    "db":"bth_order_shard4",
    "port":3306
        }  


class DBConn(object):
    
    def __init__(self,kargs):
        self.conn=MySQLdb.connect(**kargs)
        self.cur=self.conn.cursor()
    
    def __del__(self):
        self.conn.commit()
        self.cur.close()
        self.conn.close()
    #执行sql   
    def execute(self,sql,*args,**kargs):
        return self.cur.execute(sql,*args,**kargs)
    
    #返回列表
    def execute_query(self,sql,*args,**kargs):
        self.cur.execute(sql,*args,**kargs)
        tmpArry=self.cur.fetchall()
        if tmpArry==None:
            return None
        array=[]
        for i in range(len(tmpArry)):
            array.append([])
            for j in range(len(tmpArry[i])):
                array[i].append(tmpArry[i][j])
                if type(array[i][j])=="str":
                    array[i][j]=array[i][j]
                array[i][j]=str(array[i][j])
                
            return array
    
    #返回dict    
    def execute_query_dict(self,sql,*args,**kargs):
        self.cur.execute(sql,*args,**kargs)
        tmpArry=self.cur.fetchall()
        if tmpArry==None:
            return None
        array=[[str(j).strip() for j in i] for i in tmpArry]
        columnname=tuple([column[0] for column in self.cur.description])
        
        dicts=[dict(l) for l in [zip(columnname,line) for line in array]]
        return dicts

            
            
if __name__ =="__main__":
    mysqllj={
    "host":"10.100.112.44",
    "user":"booker",
    "passwd":"dg@#d1567",
    "db":"bth_order_shard4",
    "port":3306
    }          
    a=DBConn(mysqllj) 
    sql1="select * from gs_order_log where order_code='101000138574'"
    sql2="select pay_type from gs_order_inn where order_code='101000137484'"
    print a.execute_query_dict(sql1)  
    print a.execute_query_dict(sql2)  
#     conn=MySQLdb.connect(**mysqllj)
#     cur=conn.cursor()     
#     cur.execute(sql) 
#     print   cur.description
#     print cur.rowcount
#     print cur.fetchall() 
#     